﻿<cfcomponent displayname="Application" output="true" hint="Handle the application.">
	<cfset connectionPars = "useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true&useSSPSCompatibleTimezoneShift=true&useJDBCCompliantTimezoneShift=true&serverTimezone=UTC&useTimezone=false&useGmtMillisForDatetimes=false">
	<cfset this.datasources.craftdb = {
		  class: 'org.gjt.mm.mysql.Driver'
		, connectionString: 'jdbc:mysql://localhost:3306/craftdb?#connectionPars#'
		, username: 'root'
		, password: "encrypted:996a5d745d535eb79c093cb001c26569a3b00758bd960dc193dcd945c25ba900"
		
		// optional settings
		, blob:true // default: false
		, clob:true // default: false
	} />
	
	<cfset this.datasources["prodcopy"] = {
	  class: 'org.gjt.mm.mysql.Driver'
		, connectionString: 'jdbc:mysql://rdstest02.cfgpwyj6wvw4.us-east-1.rds.amazonaws.com:3306/craftdb?#connectionPars#'
		, username: 'dbadmin'
		, password: "encrypted:5f9709771d5c920725367f66feb428d0717c16cce7585a233f9b7501074aa38c"
		
		// optional settings
		, blob:true // default: false
		, clob:true // default: false
		, timezone:'UTC'
	} />
	
	<cfset this.datasources.craftdblive = {
		  class: 'org.gjt.mm.mysql.Driver'
		, connectionString: 'jdbc:mysql://rds.craftivity.me:3306/craftdb?#connectionPars#'
		, username: 'dbadmin'
		, password: "encrypted:ce41c6203f8bcbb1456924ad335772562cde5107b1491d6b846db660fb1db455"
		
		// optional settings
		, blob:true // default: false
		, clob:true // default: false
	} />

	<!--- Set up the application. --->
	<cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 2, 0, 0 ) />
	<cfset THIS.SessionManagement = true />
	<cfset THIS.SetClientCookies = false />

	<!--- Define the page request properties. --->
	<cfsetting requesttimeout="20" showdebugoutput="false" enablecfoutputonly="false" />
	<!--- --->

	<cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false"> 
		<cfreturn true />
	</cffunction>

	<!--- --->
	 
	<cffunction name="OnSessionStart" access="public" returntype="void" output="false"> 
		<cfset application.sessionStart = now() />
		<cfreturn />
	</cffunction>
	
	<!--- --->
	 
	<cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
		<cfargument	name="TargetPage" type="string"	required="true"	/>

		<cfset craftivity = false />
		<cfset populateSite = false />

		<!--- logic for craftivity --->
		<cfif structKeyExists(url,"craft") or not structKeyExists(application,"core")>
			<cfset craftivity = true />
			<!--- clear pagePool --->
			<cfset pagePoolClear() />
		</cfif>
		
		<!--- launch craftivity --->
		<cfif craftivity>
			<cfset structDelete(application,"core") />
			<cfset application.core = createObject('component','cfcs.core') />
			<cfset application.core.initCraftivity(craftivity) />
		</cfif>
		
		<!--- append global.cfc to url scope for global access --->
		<cfset structAppend(url,application.global) />

		<!--- check session --->
		<cfset application.session.startOfRequest() />
		
		<!--- check timezone --->
		<cfif structKeyExists(cookie,'clientDST') and cookie.clientDST neq "" and cookie.clientDST neq request.session.user.dst>
			<cfset $.run.it('craftivityService.updateDST',{dst=cookie.clientDST}) />
		</cfif>
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	 
	<cffunction name="OnRequest" access="public" returntype="void" output="true" hint="Fires after pre page processing is complete.">
		<cfargument	name="TargetPage" type="string"	required="true"	/>

			<cfset var local = {} />
			
			<!--- evaluate the user's timezone range(s) --->
			<cfif len(request.session.user.id) eq 32>
				<!--- update timezone range in options if cookie differs --->
				<cfset local.options = application.s.optionService.getOptions('calendar') />
				<cfset local.cookieValue = "" />
				<!--- fetch the cookie's value --->
				<cfif structKeyExists(cookie,'dstString') and cookie.dstString neq "">
					<cfset local.cookieValue = cookie.dstString />
				</cfif>
				
				<cfif local.cookieValue neq local.options.dstString>
					<cfset application.s.optionService.saveOptions('calendar',{dstString=local.cookieValue}) />
				</cfif>
			</cfif>
			
			<cfif not arguments.targetPage contains "craftivity.cfm" and not arguments.targetPage contains "hourly.cfm">
				<cfinclude template="/craftivityHeader.cfm" />
			</cfif>
			
			<cfinclude template="#ARGUMENTS.TargetPage#" />

			<cfif not arguments.targetPage contains "craftivity.cfm" and not arguments.targetPage contains "hourly.cfm">
				<cfinclude template="/craftivityFooter.cfm" />
			</cfif>
			
			<!--- boot unlogged users unless hitting the following pages --->
			<cfif len(request.session.user.id) neq 32 
				and not arguments.targetPage contains 'craftivity.cfm' 
				and not arguments.targetPage contains 'beta_signup.cfm'
				and not arguments.targetPage contains 'login.cfm'
				and not arguments.targetPage contains 'reset_password.cfm'
				and not arguments.targetPage contains 'recover_password.cfm'
				and not arguments.targetPage contains 'hourly.cfm'
			>
				<cfset local.error = "?error=notloggedin" />
				<cflocation url="#$.site.getVar('rootURL')#/craftivity.cfm#local.error#" addToken="no" />
			</cfif>
		
		<cfreturn />
	</cffunction>
	
	<!--- --->
	 
	<cffunction name="OnRequestEnd" access="public" returntype="void" output="true" hint="Fires after the page processing is complete.">
		<cfreturn />
	</cffunction>
	
	<!--- --->
	 
	<cffunction name="OnSessionEnd" access="public" returntype="void" output="false" hint="Fires when the session is terminated.">
		<cfargument name="SessionScope" type="struct" required="true" />
		<cfargument name="ApplicationScope" type="struct" required="false" default="#StructNew()#" />
		<cfset application.sessionEnd = now() />
		<cfreturn />
	</cffunction>
	
	<!--- --->
	 
	<cffunction name="OnApplicationEnd" access="public" returntype="void" output="false" hint="Fires when the application is terminated.">
		<cfargument name="ApplicationScope" type="struct" required="false" default="#StructNew()#" />
		<cfreturn />
	</cffunction>
	
	<!--- --->
	 
	<cffunction name="OnError" access="public" returntype="void" output="true" hint="Fires when an exception occures that is not caught by a try/catch.">
		<cfargument	name="Exception" type="any" required="true"	/>
		<cfargument name="EventName" type="string" required="false" default="" />
	
		<!--- log the error --->
		<cfset errorMessage = "unknown error" />
		<cfif structKeyExists(arguments,"exception") and structKeyExists(arguments.exception,"message")>
			<cfset errorMessage = arguments.exception.message />
		</cfif>
		<cfset application.helpers.log.add(
			name="error",
			method=errorMessage,
			args=cgi,
			cfcatch=arguments
		) />
		
		<cfif application.devmode>
			<cfdump var="#arguments#"><cfabort>
		<cfelse>
			<cfif cgi.cf_template_path contains 'action.cfm'>
				<cflocation url="#application.helpers.site.getVar('rootURL')#/dashboard.cfm?error=true" addToken="no" />
			<cfelse>
				<div class="error-page-text">
					<cfif errorMessage contains "not found" and errorMessage contains "Page">
						<b>Sorry, this page does not exist</b>
						</br><i class="fa fa-bomb"></i></br>
						Calmly proceed back to the dashboard.</br>Single file please.</br></br>
						<a href="#application.helpers.site.getVar('rootURL')#/dashboard.cfm">calmly redirect to dashboard</a>
					<cfelse>
						<b>Sorry, something blew up</b>
						</br><i class="fa fa-bomb"></i></br>
						The development staff has been notified
						</br>and are looking for a solution
						</br></br>
						Don't worry, we</br> <span style="font-style:italic;color:##999999;">(probably)</span> </br>won't hold you responsible
						</br>
						</br>
						<a href="#application.helpers.site.getVar('rootURL')#/dashboard.cfm">return to dashboard in shame</a>
					</cfif>
				</div>
				
				<div id="error" style="display:none;">
					<cfdump var="#cgi#">
					<cfdump var="#arguments#"><cfabort>
				</div>
			</cfif>
			<!--- do nothing? --->
		</cfif>

		<cfreturn />
	</cffunction>
	
	<!--- --->
	 
</cfcomponent>